package com.zik.rader_and_video.utils;

import cn.hutool.http.HttpRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.DeviceShadowData;
import com.huaweicloud.sdk.iotda.v5.model.DeviceShadowProperties;
import com.huaweicloud.sdk.iotda.v5.model.ShowDeviceShadowRequest;
import com.huaweicloud.sdk.iotda.v5.model.ShowDeviceShadowResponse;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.zik.rader_and_video.po.Room;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zik
 * @description
 * @time 2023/6/18 23:42
 **/
@Slf4j
public class HttpMqttUtils {

    public static Room getRooms() {
        String AK = "49DYUZERXDABP7R58CW2";
        String SK = "Bgto8QCPoAKdMR5FPdEaA6y89wKd3oWpzW1METDQ";
        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(AK)
                .withSk(SK);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("cn-north-4"))
                .build();
        ShowDeviceShadowRequest request = new ShowDeviceShadowRequest();
        request.withDeviceId("648f1d7501554a5933a1b416_zik_humiture");
        Room room = new Room();
        try {
            ShowDeviceShadowResponse response = client.showDeviceShadow(request);
            System.out.println(response.toString());
            List<DeviceShadowData> shadow = response.getShadow();
            shadow.forEach(item -> {
                Object properties = item.getReported().getProperties();
                System.out.println(properties.toString());

                String s = properties.toString();
                // 去除大括号
                String propertiesContent = s.toString().substring(1, s.length() - 1);

                // 分割属性名称和值
                String[] propertyPairs = propertiesContent.split(", ");

                // 创建属性Map
                Map<String, Integer> propertyMap = new HashMap<>();
                for (String pair : propertyPairs) {
                    String[] keyValue = pair.split("=");
                    String key = keyValue[0];
                    int value = Integer.parseInt(keyValue[1]);
                    propertyMap.put(key, value);
                }
                room.setTemperature(propertyMap.get("Temperature"));
                room.setCarbon(propertyMap.get("carbon"));
                room.setHumidity(propertyMap.get("humidity"));
                room.setMethanal(propertyMap.get("methanal"));
                room.setUltraviolet(propertyMap.get("ultraviolet"));
            });
            return room;
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }

    public static Integer getNoise() {
        String AK = "49DYUZERXDABP7R58CW2";
        String SK = "Bgto8QCPoAKdMR5FPdEaA6y89wKd3oWpzW1METDQ";
        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(AK)
                .withSk(SK);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("cn-north-4"))
                .build();
        ShowDeviceShadowRequest request = new ShowDeviceShadowRequest();
        request.withDeviceId("648f1d7501554a5933a1b416_noise");
        Map<String, Integer> propertyMap = new HashMap<>();
        try {
            ShowDeviceShadowResponse response = client.showDeviceShadow(request);
            System.out.println(response.toString());
            List<DeviceShadowData> shadow = response.getShadow();
            shadow.forEach(item -> {
                Object properties = item.getReported().getProperties();
                System.out.println(properties.toString());

                String s = properties.toString();
                // 去除大括号
                String propertiesContent = s.substring(1, s.length() - 1);

                // 分割属性名称和值
                String[] propertyPairs = propertiesContent.split(", ");

                // 创建属性Map
                for (String pair : propertyPairs) {
                    String[] keyValue = pair.split("=");
                    String key = keyValue[0];
                    int value = Integer.parseInt(keyValue[1]);
                    propertyMap.put(key, value);
                }
            });
            return propertyMap.get("noise");
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return null;
    }
}
